import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class XMLWriter {
	public static void main(String[] args) {
		try {
			Element elARoot = new Element("A");
			elARoot.setAttribute("myAttribute", "hello");
			Element elB = new Element("B");
			Element elC = new Element("C");
			Element elD = new Element("D");
			elD.addContent("This is the value of D");
			elARoot.addContent(elB);
			elARoot.addContent(elC);
			elC.addContent(elD);
			XMLOutputter outputter = new XMLOutputter();
			outputter.setFormat(Format.getPrettyFormat().setIndent("\t"));
			FileWriter Xmlfile = new FileWriter("MySecondXMLFile.xml", false);
			Document myDocument = new Document(elARoot);
			outputter.output(myDocument, System.out);
			outputter.output(myDocument, Xmlfile);
			Xmlfile.flush();
			Xmlfile.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}